package com.cct.digitalarchapi.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cct.digitalarchapi.common.Response;
import com.cct.digitalarchapi.dao.ArchCategoryMapper;
import com.cct.digitalarchapi.pojo.ArchCategory;
import com.cct.digitalarchapi.service.ArchCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @FileName ArchCategoryServiceImpl
 * @Description 档案分类实现类
 * @Author Created by chenmingzhe
 * @Date 2023/11/19
 **/
@Slf4j
@Service
public class ArchCategoryServiceImpl implements ArchCategoryService {

    @Autowired
    private ArchCategoryMapper archCategoryMapper;
    /**
     * 获取所有的档案分类
     */
    @Override
    public Response getAllCategory(int pageNum,int pageSize,String archCateType){
        try{
            Page<ArchCategory> page=new Page<>(pageNum,pageSize);
            int type=-1;
            if(StringUtils.isNotBlank(archCateType)){
                type=Integer.parseInt(archCateType);
            }
            archCategoryMapper.getAllCategory(page,type);
            return Response.success(page);
        }catch (Exception e){
            e.printStackTrace();
            log.error("查询所有档案分类失败：{}",e.getMessage());
            return Response.error("查询所有档案分类失败");
        }

    }

    /**
     * 新增部门分类
     */
    @Override
    public Response addCategory(ArchCategory archCategory) {
        try{
            int num = archCategoryMapper.queryHaveSameName(archCategory.getArchCateName());
            if (num >= 1) {
                return Response.bad("档案分类名字已存在，请重新输入！");
            }
            //取最后一条记录的code
            ArchCategory category=archCategoryMapper.selectLastCategory();
            if(category!=null){
                int archCateCode=Integer.parseInt(category.getArchCateCode())+1;
                if(archCateCode>=10){
                    archCategory.setArchCateCode(String.valueOf(archCateCode));
                }else{
                    archCategory.setArchCateCode("0"+String.valueOf(archCateCode));
                }
            }else{
                archCategory.setArchCateCode("01");
            }
            archCategoryMapper.addCategory(archCategory);
            return Response.success("");
        }catch (Exception e){
            e.printStackTrace();
            log.error("新增档案分类失败：{}",e.getMessage());
            return Response.error("新增档案分类失败");
        }
    }

    /**
     * 删除一个档案分类
     *
     * @param archCateId
     */
    @Override
    public Response deleteCategory(int archCateId) {
        try {
            archCategoryMapper.deleteCategory(archCateId);
            return Response.success("");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("删除档案分类失败：{}",e.getMessage());
            return Response.error("删除档案分类失败");
        }
    }

    @Override
    public Response updateCategory(ArchCategory archCategory) {
        try {
            archCategoryMapper.updateCategory(archCategory);
            return Response.success("");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("修改档案分类失败：{}",e.getMessage());
            return Response.error("修改档案分类失败");
        }
    }

}
